home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 147
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan).7z
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan) (Track 1).bin
/
tools
/
zmc3v078
/
zmc3v078.lzh
/
SRCSV078.LZH
/
ETC.C
< prev
next >
Wrap
C/C++ Source or Header
|
2000-05-31
|
7KB
|
388 lines
/* =======================
low_level functions
======================= */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef MEMDEBUG
#include <string.h>
#endif
#include "config.h"
#define FILENOTFOUND 1
#define NONZMD 2
#define ZMDVERERR 3
#define READERR 4
#define WRITEERR 5
#define FLUSHERR 6
#define SWERR 7
#define TOOMANYFILES 8
#define WAVNOTDEFIND 9
#define NOMEMORY 10
#define ILLEGALSWITCH 11
#define ILLEGALMEMORY 12
#ifdef MEMDEBUG
typedef struct memDebugData {
struct memDebugData *next;
/* struct memDebugData *prev; */
DWORD size; /* malloced size */
void *adrs; /* malloced area */
char *label; /* label name */
} MEMDEBUGDATA;
void memdebug(void);
MEMDEBUGDATA *searchHimself(void *adrs, char *label, char f);
#endif
void efree(void *ptr, char *label);
void *emalloc(size_t n, char *label);
void *erealloc(void *ptr,size_t n, char *label);
void *emarealloc(void *ptr,size_t n, char *label);
void fatal(int ret,char *fmt, ...);
inline WORD getWordAlign(UBYTE*);
inline DWORD getDwordAlign(UBYTE*);
inline void putWordAlign(UBYTE*,WORD);
inline void putDwordAlign(UBYTE*,DWORD);
inline WORD getWord(UBYTE*);
inline DWORD getDword(UBYTE*);
inline void putWord(UBYTE*,WORD);
inline void putDword(UBYTE*,DWORD);
#ifndef UNIXY_OS
int iskanji(int a);
#endif
#ifndef UNIXY_OS
int iskanji(int a) /* only SJIS is supported. */
{
return ((0x81 <= a && a <= 0x9F) || (0xE0 <= a && a <= 0xFC))? 1 : 0;
}
#endif
#ifdef UNIXY_OS
/* ---- for use of `ntohs' ,`ntohl', `htons', and `htonl' ---- */
#ifdef FreeBSD
#include <sys/param.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#endif
#endif /* UNIXY_OS */
#ifdef MEMDEBUG
static MEMDEBUGDATA memMaster = {
NULL, /* next */
0, /* size */
NULL, /* adrs */
NULL /* label */
};
void memdebug(void)
{
MEMDEBUGDATA *m = &memMaster;
int c = 0;
while (m->next != NULL) {
m = m->next;
fprintf(stderr,"%p %d %s\n",m->adrs, m->size, m->label);
c++;
}
if (c) {
fprintf(stderr, "%d memory blocks are still allocated.\n", c);
}
}
#endif
void efree(void *ptr, char *label)
{
#ifdef MEMDEBUG
MEMDEBUGDATA *m, *p, *n;
if (ptr) {
p = searchHimself(ptr, label, 'F');
m = p->next;
n = m->next;
free(m->label);
free(m);
p->next = n;
}
#endif
if (ptr) {
free(ptr);
}
}
/* =======================================
malloc function with error handling
======================================= */
void *emalloc(size_t n, char *label)
{
void *pt;
#ifdef MEMDEBUG
MEMDEBUGDATA *m = &memMaster;
#endif
if ( (pt = malloc(n)) == NULL ) {
fatal(NOMEMORY,"malloc failed.\n");
}
#ifdef MEMDEBUG
while (m->next != NULL) {
m = m->next;
}
m->next = malloc(sizeof(MEMDEBUGDATA) * 1);
if (m->next == NULL) {
fatal(NOMEMORY,"malloc failed.\n");
}
m = m->next;
m->next = NULL;
m->size = n;
m->adrs = pt;
m->label = malloc(sizeof(char) * strlen(label) + 1);
if (m->label == NULL) {
fatal(NOMEMORY,"malloc failed.\n");
}
strcpy(m->label, label);
#endif
return pt;
}
/* ========================================
search malloced table before
If it found himself, return the "previous" pointer
or NULL
======================================== */
#ifdef MEMDEBUG
MEMDEBUGDATA *searchHimself(void *adrs, char *label, char f)
{
MEMDEBUGDATA *m = &memMaster;
MEMDEBUGDATA *p = NULL; /* previous adrs */
MEMDEBUGDATA *r = NULL; /* return value */
while (m->next != NULL) {
p = m;
m = m->next;
if (m->adrs == adrs) {
r = p;
break;
}
}
if (r == NULL) {
memdebug();
fatal (ILLEGALMEMORY,"MEMORY LEAK FOUND: malloced table search failed.\n(%p [%c]%s)\n",adrs,f,label);
}
return r;
}
#endif
/* ========================================
realloc function with error handling
======================================== */
void *erealloc(void *ptr, size_t n, char *label)
{
void *pt;
#ifdef MEMDEBUG
MEMDEBUGDATA *m = searchHimself(ptr, label, 'r');
#endif
if ( (pt = realloc(ptr,n)) == NULL ) {
#ifdef MEMDEBUG
memdebug();
fprintf(stderr,"%s: %p -> %d",label, ptr,n);
#endif
fatal(NOMEMORY,"realloc failed.\n");
}
#ifdef MEMDEBUG
m = m->next;
m->size = n;
m->adrs = pt;
m->label = realloc(m->label, strlen(m->label) + strlen(label) + 1);
if (m->label == NULL) {
fatal(NOMEMORY,"realloc failed.\n");
}
strcat(m->label, label);
#endif
return pt;
}
void *emarealloc(void *ptr, size_t n, char *label)
{
if ( !ptr ) {
return emalloc(n,label);
} else {
return erealloc(ptr,n,label);
}
}
/* ========================
fatal error
======================== */
void fatal(int ret,char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
exit(ret);
}
/* =========================
read WORD,DWORD data
big/little endian OK
for even-byte align only
input:pointer
output:number
========================= */
inline WORD getWordAlign(UBYTE *zmd)
{
#ifdef X68000
return *(WORD*)zmd;
#else
WORD data;
data = *zmd++ << 8;
data += *zmd;
return data;
#endif
}
inline DWORD getDwordAlign(UBYTE *zmd)
{
#ifdef X68000
return *(DWORD*)zmd;
#else
int data;
data = (*zmd++ << 24);
data += (*zmd++ << 16);
data += (*zmd++ << 8);
data += *zmd;
return data;
#endif
}
/* ============================
write WORD,DWORD data
big/little endian OK
for even-byte align only
input:number,pointer
============================ */
inline void putWordAlign(UBYTE *pos,WORD data)
{
#ifdef UNIXY_OS
WORD val;
UBYTE *ptr;
val = htons(data);
ptr = (UBYTE*)&val;
*pos++ = *ptr++;
*pos++ = *ptr++;
#elif defined X68000
*(WORD*)pos = data;
#else
*pos++ = data >> 8;
*pos = (data & 0xFF);
#endif
}
inline void putDwordAlign(UBYTE *pos,DWORD data)
{
#ifdef UNIXY_OS
DWORD val;
UBYTE *ptr;
val = htonl(data);
ptr = (UBYTE*)&val;
*pos++ = *ptr++;
*pos++ = *ptr++;
*pos++ = *ptr++;
*pos++ = *ptr++;
#elif defined X68000
*(DWORD*)pos = data;
#else
*pos++ = data >> 24;
*pos++ = (data >> 16) & 0xFF;
*pos++ = (data >> 8) & 0xFF;
*pos = (data & 0xFF);
#endif
}
/* ============================
read WORD,DWORD data
input:pointer
output:number
============================ */
inline WORD getWord(UBYTE *zmd)
{
WORD data;
data = *zmd++ << 8;
data |= *zmd;
return data;
}
inline DWORD getDword(UBYTE *zmd)
{
DWORD data;
data = (*zmd++ << 24);
data |= (*zmd++ << 16);
data |= (*zmd++ << 8);
data |= *zmd;
return data;
}
/* ============================
write WORD,DWORD data
input:number,pointer
============================ */
inline void putWord(UBYTE *pos,WORD data)
{
*pos++ = data >> 8;
*pos = (data & 0xFF);
}
inline void putDword(UBYTE *pos,DWORD data)
{
*pos++ = data >> 24;
*pos++ = (data >> 16) & 0xFF;
*pos++ = (data >> 8) & 0xFF;
*pos = (data & 0xFF);
}